package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.pojo.EventsPojo;
import com.gxwgy.wg2404.pojo.UsersPojo;
import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 姚相国
 * @version 1.0
 * @className AdminDashboardServlet
 * @date 2024/12/18 02:24
 */
@WebServlet({"/admin/dashboard"})
public class AdminDashboardServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");

        // 检查用户是否已登录
        if (username == null) {
            // 如果未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/index.jsp");
            return; // 防止后续代码继续执行
        }

        // 如果用户已登录但不是管理员，限制访问
        if (!"root".equals(username) && !"admin1".equals(username)) {
            // 如果不是管理员，重定向到主页
            response.sendRedirect(request.getContextPath() + "/index.jsp");
            return;
        }

        // 如果是管理员，获取活动数据
        List<EventsPojo> events = getEvents();
        List<UsersPojo> users = getUsers();

        // 将数据传递给 JSP 页面
        request.setAttribute("events", events);
        request.setAttribute("users", users);

        // 转发到 JSP 页面
        request.getRequestDispatcher("/administrator.jsp").forward(request, response);
    }


    private List<EventsPojo> getEvents() {
        List<EventsPojo> eventList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "select * from events"; // 查询所有活动
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                EventsPojo event = new EventsPojo();
                event.setId(rs.getString("id"));
                event.setName(rs.getString("name"));
                event.setDescription(rs.getString("description"));
                event.setStart_time(rs.getString("start_time"));
                event.setEnd_time(rs.getString("end_time"));
                event.setLocation(rs.getString("location"));
                event.setStatus(rs.getString("status"));
                event.setCapacity(rs.getInt("capacity"));
                event.setCurrent_enrollment(rs.getInt("current_enrollment"));
                event.setUpdated_at(rs.getString("updated_at"));
                event.setCreated_at(rs.getString("created_at"));


                eventList.add(event);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return eventList;
    }

    // 获取用户列表
    private List<UsersPojo> getUsers() {
        List<UsersPojo> usersList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "select * from users"; // 查询所有用户
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                UsersPojo user = new UsersPojo();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setEmail(rs.getString("email"));
                user.setRole(rs.getString("role"));
                user.setUpdated_at(rs.getString("created_at"));
                user.setUpdated_at(rs.getString("updated_at"));

                usersList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return usersList;
    }

}


